package com.android.launcher3.widget;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.x.component.widget.MediaWidgetHost;
import com.x.component.widget.MediaWidgetInfo;
import com.x.component.widget.MediaWidgetTool;

public class AudioWidget extends FrameLayout {
    private View vPre;
    private View vPlay;
    private View vNext;
    private TextView tvName;
    private TextView tvArtist;
    private View vAlbum;
    private View v_play;
    private RelativeLayout layoutControl;

    public AudioWidget(@NonNull Context context) {
        super(context);
        init(null);
    }

    public AudioWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public AudioWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    void init(@Nullable AttributeSet attrs) {
        boolean horizontalType = false;
        if (attrs != null) {
            TypedArray a = getContext().getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.AudioWidget,
                    0, 0);

            try {
                horizontalType = a.getBoolean(R.styleable.AudioWidget_horizontal_type, false);
            } finally {
                a.recycle();
            }
        }

        LayoutInflater.from(getContext()).inflate(horizontalType ? R.layout.layout_audio_horzontal : R.layout.layout_audio, this, true);

        vPre = findViewById(R.id.v_pre);
        vPlay = findViewById(R.id.v_play);
        vNext = findViewById(R.id.v_next);
        tvName = findViewById(R.id.tv_name);
        tvArtist = findViewById(R.id.tv_artist);
        v_play = findViewById(R.id.v_play);
        vAlbum = findViewById(R.id.v_album);
        layoutControl = findViewById(R.id.layout_control);
        vPre.setOnClickListener(view -> {
            Log.d("MediaWidgetLauncher", "onClick");
            MediaWidgetHost.getInstance().sendMediaButtonEventPrevious();
        });
        vNext.setOnClickListener(view -> {
            Log.d("MediaWidgetLauncher", "onClick");
            MediaWidgetHost.getInstance().sendMediaButtonEventNext();
        });
        vPlay.setOnClickListener(view -> {
            Log.d("MediaWidgetLauncher", "onClick");
            MediaWidgetHost.getInstance().sendMediaButtonEventPlay();
        });


        try {
            //MediaWidget
            MediaWidgetHost mediaWidgetHost = MediaWidgetHost.getInstance();
            mediaWidgetHost.init(getContext());
            mediaWidgetHost.setMediaWidgetTool(new MediaWidgetTool() {
                @Override
                public void onMediaWidgetInfoChanged(MediaWidgetInfo mediaWidgetInfo) {
                    Log.d("MediaWidgetLauncher", "onMediaWidgetInfoChanged: " + mediaWidgetInfo.toString());
                    String title = "Unknown";
                    String artist = "Unknown";
                    if (!TextUtils.isEmpty(mediaWidgetInfo.getTitle())) {
                        title = mediaWidgetInfo.getTitle();
                    }
                    if (!TextUtils.isEmpty(mediaWidgetInfo.getAritist())) {
                        artist = mediaWidgetInfo.getAritist();
                    }
                    tvName.setText(title);
                    tvArtist.setText(artist);
                }

                @Override
                public void onMediaWidgetPlaybackStatusChanged(int status) {
                    Log.d("MediaWidgetLauncher", "onMediaWidgetPlaybackStatusChanged: " + status);
                    if (status == 1) {
                        v_play.setBackground(getContext().getResources().getDrawable(R.drawable.ic_pause));
                    } else {
                        v_play.setBackground(getContext().getResources().getDrawable(R.drawable.ic_play));
                    }

                }
            });
        } catch (Throwable t) {
            t.printStackTrace();
        }
        Log.d("MediaWidgetLauncher", "startMediaWidgetInfoTracking.");
//        mediaWidgetHost.startMediaWidgetInfoTracking();
//        mediaWidgetHost.stopMediaWidgetInfoTracking();

    }

}
